"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var BaseController_1 = require('./BaseController');
var AddProjectDto_1 = require('../dtos/projects/AddProjectDto');
var UpdateProjectDto_1 = require('../dtos/projects/UpdateProjectDto');
var ProjectsService_1 = require('../services/ProjectsService');
var ProjectController = (function (_super) {
    __extends(ProjectController, _super);
    function ProjectController(app) {
        _super.call(this, app);
        var $this = this;
        app.post(this.domain + "/projects", function (req, res) {
            $this.handerErr(req, res, function (user) {
                $this.addProjects(req, res, user);
            }, true);
        });
        app.delete(this.domain + "/projects/:id", function (req, res) {
            $this.handerErr(req, res, function () {
                $this.deleteProject(req, res);
            }, true);
        });
        app.get(this.domain + "/projects", function (req, res) {
            $this.handerErr(req, res, function () {
                $this.getProjects(req, res);
            }, true);
        });
        app.get(this.domain + "/projects/user", function (req, res) {
            $this.handerErr(req, res, function (user) {
                $this.getMyProjects(req, res, user);
            }, true);
        });
        app.get(this.domain + "/projects/user/:id", function (req, res) {
            $this.handerErr(req, res, function () {
                $this.getProjectsByUserId(req, res);
            });
        });
        app.put(this.domain + "/projects/:id", function (req, res) {
            $this.handerErr(req, res, function () {
                $this.updateProject(req, res);
            }, true);
        });
        app.get(this.domain + "/projects/:id", function (req, res) {
            $this.handerErr(req, res, function () {
                $this.getProject(req, res);
            }, true);
        });
        this.projectsService = new ProjectsService_1.ProjectsService();
    }
    ProjectController.prototype.addProjects = function (req, res, user) {
        var userId = user._id;
        this.projectsService.add(AddProjectDto_1.AddProjectDto.toProject(userId, req.body), function (err, project) {
            res.send(JSON.stringify(project));
        });
    };
    ProjectController.prototype.getProject = function (req, res) {
        _super.prototype.initResponse.call(this, res);
        var id = req.params.id;
        this.projectsService.getProject(id, function (err, project) {
            res.send(JSON.stringify(project));
        });
    };
    ProjectController.prototype.getProjects = function (req, res) {
        _super.prototype.initResponse.call(this, res);
        this.projectsService.getProjects(function (err, projects) {
            res.send(JSON.stringify(projects));
        });
    };
    ProjectController.prototype.getProjectsByUserId = function (req, res) {
        _super.prototype.initResponse.call(this, res);
        var userId = req.params.id;
        this.projectsService.getMyProjects(userId, function (err, projects) {
            res.send(JSON.stringify(projects));
        });
    };
    ProjectController.prototype.getMyProjects = function (req, res, user) {
        var userId = user._id;
        this.projectsService.getMyProjects(userId, function (err, projects) {
            res.send(JSON.stringify(projects));
        });
    };
    ProjectController.prototype.updateProject = function (req, res) {
        var id = req.params.id;
        var project = UpdateProjectDto_1.UpdateProjectDto.toProject(id, req.body);
        this.projectsService.updateProject(project, function (err, project) {
            res.send(JSON.stringify(project));
        });
    };
    ProjectController.prototype.deleteProject = function (req, res) {
        var id = req.params.id;
        this.projectsService.deleteProject(id, function (err, project) {
            res.send(JSON.stringify(project));
        });
    };
    return ProjectController;
}(BaseController_1.BaseController));
exports.ProjectController = ProjectController;
